#!/usr/bin/python
# coding=utf8
###############################################################################
#                                                                             #
#    Copyleft 2007 Danil Dotsenko                                       #
#                                                                             #
#    This program is free software; you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation; either version 2 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program; if not, write to the Free Software              #
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA #
#                                                                             #
###############################################################################                                   #
#             Danil Dotsenko, dd@accentsolution.com                           #

#from __future__ import division
import karambino

def debug():
	return False

###########################################################################
# applet settings go here, and are accessible from everywhere as global
class configClass:
	'''
	An all-encoupasing, application-wide configuration storage location.
	This will be shared even between concurrently running themes.
	This is the equivalent of root directory of Windows registry.
	'''
	def __init__(self):
		self.common={
			'name':'TileList Example',
			'version':"June, 2007",
			'author':"Danil (Suslik) Dotsenko (dd@accentsolution.com)",
			'cleanStart':False,
			'interval':10000,
			}
		self.lang = None
		self.objects = {}
#
##########################################################

def setupObjects(widget):
	w, h, th = 130, 4, 60 #width, # of tiles, height of one.
	karambino.resizeWidget(widget, w, h*th+30)
	# image = karambino.imageClass('images/green.png', 0,0,100,90,widget=widget)

	back = karambino.textClass('', w=w, h=h*th+30, widget=widget)
	back.button = karambino.imageClass('alphagray.png', w=40, h=20, parent=back)
	back.button.move(0,-5, 's','s',redraw=False)
	back.button.label = karambino.textClass('refresh', w=80, h=20, parent=back.button)
	back.button.label.move(0,0, 'center','center',redraw=False)
	back.button.label.text.color(255,255,255,redraw=False)
	listbox = karambino.tileListClass(parent=back)

	# note ", *args, **kw" keep it in your fn's too.
	
	def buildtile(parent, tilenum, *args, **kw):
		# you can use globals to get your settings from your main scope into here.
		# the following variables of parent may also help you:
		# parent.width = width of the tileList container.
		# parent.visibletiles
		# parent.tileheight
		#
		# this is going to be tile's background and "arent" for all other elements.
		tile = karambino.textClass('', w=parent.width, h=parent.tileheight, parent=parent)
		tile.back = karambino.imageClass('alphagray.png', w=parent.width-2, h=parent.tileheight-2, parent=tile)
		tile.back.move(1,1, 'nw', 'nw',redraw=False) # move 10 pixels from NorthWest corner of parent.
		tile.logo = karambino.imageClass('blue.png', w=40, h=40, parent=tile)
		tile.logo.move(10,10, 'nw', 'nw',redraw=False) # move 10 pixels from NorthWest corner of parent.
		tile.title = karambino.textClass('', w=60, h=30, parent=tile)
		tile.title.text.color(255,255,255, redraw=False)
		tile.title.move(60,10, 'nw', 'nw', redraw=False)
		return tile
	listbox.buildtile = buildtile

	def populatetile(tile, dataunit, *args, **kw):
		'''
		tile - pointer to tile object.
		dataunit - pointer to data object from which to collect the pieces and populate the tile.
		'''
		tile.title.text(dataunit,redraw=False)
		# you don't need to redraw for the whole function because right after it,
		# we move the tile and redraw anyway.
	listbox.populatetile = populatetile

	data = [] #'Tile 1','Tile 2','Tile 3','Tile 4','Tile 5','Tile 6','Tile 7']
	listbox.initialize(listbox, data=data, x=0, y=0, w=w, h=h, tileheight=th)
	back.button.clicked.btn[1].signalset('refreshlist', listbox.refresh)

##################################################################
# Testing & helper functions.
#
def notificationWindow(message, title="", mode=None):
	'''
	This uses KDE kdialog app and asks for main body message and window title.
	App name is attached automatically
	'''
	global appConfig
	myCommand = 'kdialog --caption "'+appConfig.common['name']+'" --title "'+title+'" --msgbox "'+message+'"'
	karambino.execute(myCommand)
#
##################################################################

##################################################################
# KARAMBA's built-in functions
#
#this is called when you widget is initialized
def initWidget(widget):
	global appConfig
	
	appConfig=configClass()
	success = True
	appConfig.common['cleanStart'] = True
	# createMenus(widget)
	setupObjects(widget)
	karambino.redrawWidget(widget)

def widgetUpdated(widget):
	global appConfig
	if appConfig.common['cleanStart']:
		karambino.changeInterval(widget, appConfig.common['interval'])

def widgetClosed(widget):
	global appConfig
	try:
		a = karambino.getcfg('threadfarm')
		a.stop()
	except:
		pass

def itemDropped(widget, dropText, x=0, y=0):
	karambino.itemDropped(widget, dropText, x, y)

#  botton = button clicked:
#                    1 = Left Mouse Button
#                    2 = Middle Mouse Button
#                    3 = Right Mouse Button
#                    4,5 = Scroll wheel up and down
def widgetClicked(widget, x, y, button):
	karambino.widgetClicked(widget, x, y, button)

# NOTE you must use attachClickArea() to make a meterClicked work
def meterClicked(widget, meter, button):
	karambino.meterClicked(widget, meter, button)

def keyPressed(widget, meter, char):
	karambino.keyPressed(widget, meter, char)

def themeNotify(widget, caller, message):
	pass
	
def widgetMouseMoved(widget, x, y, button):
	#Warning:  Don't do anything too intensive here
	karambino.widgetMouseMoved(widget, x, y, button)

# This will be printed when the widget loads.
print "Karambino example is loaded."
